package net.sunbeta.test.jmx ;

import java.io.File ;

import javax.mail.internet.MimeMessage ;

import org.apache.log4j.Logger ;
import org.springframework.jmx.export.annotation.ManagedAttribute ;
import org.springframework.jmx.export.annotation.ManagedOperation ;
import org.springframework.jmx.export.annotation.ManagedResource ;
import org.springframework.mail.SimpleMailMessage ;
import org.springframework.mail.javamail.JavaMailSender ;
import org.springframework.mail.javamail.MimeMessageHelper ;
import org.springframework.mail.javamail.MimeMessagePreparator ;

@ManagedResource(objectName = "testApp:name=JMXControl", description = "A JMX-managed bean")
public class JMXControl {

    private String              name ;
    private JavaMailSender      mailSender ;
    private SimpleMailMessage   message ;
    private static final Logger LOG = Logger.getLogger(JMXControl.class) ;

    public JMXControl(JavaMailSender p_emailSender, SimpleMailMessage p_message) {
        super() ;
        mailSender = p_emailSender ;
        message = p_message ;
    }

    @ManagedAttribute
    @ManagedOperation(description = "get name")
    public String getName() {
        return name ;
    }

    @ManagedAttribute
    @ManagedOperation(description = "print name")
    public void printHello() {
        System.out.println("Hello, " + name) ;
    }

    @ManagedAttribute
    @ManagedOperation(description = "print string")
    public void printHello(String theName) {
        System.out.println("Hello, " + theName) ;
    }

    @ManagedAttribute
    @ManagedOperation(description = "set name")
    public void setName(String name) {
        this.name = name ;
    }

    @ManagedAttribute
    @ManagedOperation(description = "send email")
    public void sendMail() {
        try {
            sendMails() ;
        } catch (Exception e) {
            e.printStackTrace() ;
        }
    }

    public void sendMails() {
        final String subject = message.getSubject() + "FXPlayGround Test Mail" ;

        MimeMessage mimeMessage = mailSender.createMimeMessage() ;
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8") ;
                messageHelper.setSubject(subject) ;
                messageHelper.setTo(message.getTo()) ;

                messageHelper.setFrom(message.getFrom()) ;
                messageHelper.setText("Test", "<b>Hello</b><br/> <p>I am a test mail </p><br/> FxPlayGround") ;
                File f = new File("ftpFiles/sourceFile.txt") ;
                File f2 = new File("ftpFiles/flagFile.txt") ;
                messageHelper.addAttachment(f.getName(), f) ;
                messageHelper.addAttachment(f2.getName(), f2) ;
            }
        } ;

        LOG.info("---------- Sending Email--------") ;
        // mailSender.send(message);
        mailSender.send(preparator) ;
        System.out.println("Mail sent") ;

    }
}
